<?php

class BuybidController extends Controller{
		public $_buyBidPackIds = '10,11,77,12,13,14';
		public function init(){
        parent::init();
		// $this->layout = 'splash';
//         if(Yii::app()->params['server_environment']=='live'){
             $this->https();
//         }
	}
		public function actionIndex(){
			$this->breadcrumbs = array("Bid Packs");
			if(yii::app()->user->isGuest){
				Yii::app()->runController('login/index/url/buybid');
				die;
			}
			$model=new ProductInfo();
			$bidpack=$model->findAll('id in ('.$this->_buyBidPackIds.')');
			$fee=new ProductFee();
			$bid_fee=$fee->findAll('product_id in ('.$this->_buyBidPackIds.') and fee_name="value_price"');
			$bidinfo=new ProductParame();
			$num = $fee->findAll('product_id in ('.$this->_buyBidPackIds.') and fee_name="bids"');
			$ProductResource=new ProductResource();
			$resource= $ProductResource->findAll('product_id in ('.$this->_buyBidPackIds.')');
			foreach ($resource as $k => $v) {
				$resource_id[] = $v->resource_id;
			}
			$resource_img= new ResourceImg();
			$url=$resource_img->findAll('id in ('.implode(",",$resource_id).')');
			// $this->breadcrumbs = array("Buy Bids");
			$this->render('index',array('url'=>$url,'bids'=>$bidpack,'num'=>$num,'fee'=>$bid_fee));
		}

		public function actionPayment(){
			$this->breadcrumbs = array("Bid Packs","PAYMENT BID PACK");
			if(empty(yii::app()->user->id)){
				Yii::app()->runController('login/index/url/buybid');
				die;
			}
			$customerOrderBillingInfoModel = new CustomerOrderBillingInfo('checkout');

			$model = $customerOrderBillingInfoModel->find('customer_id = '.Yii::app()->user->id);

			if(empty($model)){
				$model = $customerOrderBillingInfoModel;
			}else{
				$model->scenario = 'checkout';
			}
			$model->credit_card_exp_month = date('m');
			$id = Yii::app()->request->getParam('id',null);
			$_buyBidPackIdsarr= explode(",", $this->_buyBidPackIds);
			if(!in_array($id, $_buyBidPackIdsarr)){
				Yii::app()->getRequest()->redirect(Yii::app()->createUrl('buybid'));
				die;
			}

			$buyBidPackInfo = $this->getBidPackInfo();
			// var_dump($buyBidPackInfo['bidtypearray']);die;
			$ProductResource=new ProductResource();
			$resource= $ProductResource->find('product_id='.$id);
			$resource_img= new ResourceImg();
			$url=$resource_img->find('id = '.$resource->resource_id);
			
			$this->render('payment',array(
				'buyBidPackInfo' => $buyBidPackInfo,	
				'bidPackId' => 	$id,	
				'model' => $model,
				'url'=>$url->path,
			));
		}
		public function actionCouponcode(){

			$this->breadcrumbs = array("Bid Packs","PAYMENT BID PACK");
			if(empty(yii::app()->user->id)){
				Yii::app()->runController('login/index/url/buybid');
				die;
			}
			$customerOrderBillingInfoModel = new CustomerOrderBillingInfo('checkout');

			$model = $customerOrderBillingInfoModel->find('customer_id = '.Yii::app()->user->id);

			if(empty($model)){
				$model = $customerOrderBillingInfoModel;
			}else{
				$model->scenario = 'checkout';
			}
			$model->credit_card_exp_month = date('m');
			//$id = Yii::app()->request->getParam('id',null);
			// $_buyBidPackIdsarr= explode(",", $this->_buyBidPackIds);
			// if(!in_array($id, $_buyBidPackIdsarr)){
			// 	Yii::app()->getRequest()->redirect(Yii::app()->createUrl('buybid'));
			// 	die;
			// }
			//$buyBidPackInfo = $this->getBidPackInfo();
			// $ProductResource=new ProductResource();
			// $resource= $ProductResource->find('product_id='.$id);
			// $resource_img= new ResourceImg();
			// $url=$resource_img->find('id = '.$resource->resource_id);
			$couponCode = Yii::app()->request->getParam('couponcode',null);
			
			if(!empty($couponCode)){
				$couponCodeService = new CouponCodeVerificationService();
				
				$couponCodeInfo = $couponCodeService->setCouponCode($couponCode)->setCouponCodeData()->getCouponCodeData();

	            $return = $couponCodeService->verification();

	            if(!$return){
	                Yii::app()->session['_responder_mark'] = 'mark';
	                $this->redirect(Yii::app()->baseUrl.'/buybid/index');
	            }
	            unset(Yii::app()->session['_responder_mark']);


				$buyBidPackInfo['bidtypearray'] = array($couponCode=>$couponCode);
				$buyBidPackInfo['biddirectly'] = array($couponCode=>$couponCodeInfo);
			}
			$url=Yii::app()->baseUrl."/images/buybid/bids_code.jpg";
			$this->render('couponcode',array(
				'buyBidPackInfo' => $buyBidPackInfo,	
				'bidPackId' => "1",	
				'model' => $model,
				'url'=>$url,
				'couponcode' => $couponCode
			));
		}
		public function GetBidPackInfo(){
			$model=new ProductInfo();
			$parame=new ProductParame();
			$fee=new ProductFee();
			$bids=$model->findAll('id in ('.$this->_buyBidPackIds.')');
			foreach ($bids as $k => $v) {
				$id = $v->id;
				$bidtypearray[$id]=$v;
				$parame_data=$fee->findAllByAttributes(array('product_id'=>$id,'fee_name'=>'value_price'));
				$fee_data=$fee->findAllByAttributes(array('product_id'=>$id,'fee_name'=>'bids'));
				foreach ($parame_data as $key => $val) {
					$biddirectly[$id]=$val;
				}
				foreach ($fee_data as $keyfee => $valfee) {
					$bidfee[$id]=$valfee;
				}	
			} 
			$getBidPackInfo=array(
				'bidtypearray' => $bidtypearray,
				'biddirectly' => $biddirectly,
				'bidfee'=>$bidfee
				);
			return $getBidPackInfo;
		}
		
		public function actionBidpackauction(){
		    
		}
}